

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>agpy.blackbody &mdash; agpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../_static/extra.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="agpy 0.1.2 documentation" href="../../index.html" />
    <link rel="up" title="agpy" href="../agpy.html" />
     
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setDomainName', 'pyspeckit.bitbucket.org']);
      _gaq.push(['_setAllowHash', false]);
      _gaq.push(['_trackPageview']);


    </script>
    <link rel="stylesheet" type="text/css" href="../../_static/extra.css" />
  </head>
  <body>
    <div class="header-wrapper">
      <div class="header">
        <h1><a href="../../index.html">agpy 0.1.2 documentation</a></h1>
        <div class="rel">
          <a href="http://agpy.googlecode.com">agpy Home </a>  |
          <a href=../../index.html>Docs Home </a>  |
          <a href="http://code.google.com/p/agpy/w/list">Wiki</a>  |
          <a href=../../search.html>Search </a>
        </div>
       </div>
    </div>

    <div class="content-wrapper">
      <div class="content">
        <div class="document">
            
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for agpy.blackbody</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">============================</span>
<span class="sd">Simple black-body calculator</span>
<span class="sd">============================</span>

<span class="sd">Includes both wavelength and frequency blackbody functions.  Has flexible</span>
<span class="sd">units.  Also allows for a few varieties of modified blackbody.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="n">exp</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">exp</span>

<span class="n">unitdict</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s">&#39;cgs&#39;</span><span class="p">:{</span> <span class="s">&#39;h&#39;</span><span class="p">:</span><span class="mf">6.626068e-27</span><span class="p">,</span>
            <span class="s">&#39;k&#39;</span><span class="p">:</span><span class="mf">1.3806503e-16</span><span class="p">,</span>
            <span class="s">&#39;c&#39;</span><span class="p">:</span><span class="mf">2.99792458e10</span><span class="p">,</span>
            <span class="s">&#39;mh&#39;</span><span class="p">:</span><span class="mf">1.67262158e-24</span> <span class="o">*</span> <span class="mf">1.00794</span><span class="p">,</span>
            <span class="s">&#39;length&#39;</span><span class="p">:</span><span class="s">&#39;cm&#39;</span><span class="p">},</span>
        <span class="s">&#39;mks&#39;</span><span class="p">:{</span> <span class="s">&#39;h&#39;</span><span class="p">:</span><span class="mf">6.626068e-34</span><span class="p">,</span>
            <span class="s">&#39;k&#39;</span><span class="p">:</span><span class="mf">1.3806503e-23</span><span class="p">,</span>
            <span class="s">&#39;c&#39;</span><span class="p">:</span><span class="mf">2.99792458e8</span><span class="p">,</span>
            <span class="s">&#39;mh&#39;</span><span class="p">:</span><span class="mf">1.67262158e-27</span> <span class="o">*</span> <span class="mf">1.00794</span><span class="p">,</span>
            <span class="s">&#39;length&#39;</span><span class="p">:</span><span class="s">&#39;m&#39;</span><span class="p">}</span>
        <span class="p">}</span>

<span class="n">frequency_dict</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s">&#39;Hz&#39;</span><span class="p">:</span><span class="mf">1.0</span><span class="p">,</span>
        <span class="s">&#39;kHz&#39;</span><span class="p">:</span><span class="mf">1e3</span><span class="p">,</span>
        <span class="s">&#39;MHz&#39;</span><span class="p">:</span><span class="mf">1e6</span><span class="p">,</span>
        <span class="s">&#39;GHz&#39;</span><span class="p">:</span><span class="mf">1e9</span><span class="p">,</span>
        <span class="s">&#39;THz&#39;</span><span class="p">:</span><span class="mf">1e12</span><span class="p">,</span>
        <span class="p">}</span>

<div class="viewcode-block" id="blackbody"><a class="viewcode-back" href="../../agpy.html#agpy.blackbody.blackbody">[docs]</a><span class="k">def</span> <span class="nf">blackbody</span><span class="p">(</span><span class="n">nu</span><span class="p">,</span><span class="n">temperature</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">units</span><span class="o">=</span><span class="s">&#39;cgs&#39;</span><span class="p">,</span><span class="n">frequency_units</span><span class="o">=</span><span class="s">&#39;Hz&#39;</span><span class="p">,</span> <span class="n">normalize</span><span class="o">=</span><span class="nb">max</span><span class="p">):</span>
    <span class="c"># load constants in desired units</span>
    <span class="n">h</span><span class="p">,</span><span class="n">k</span><span class="p">,</span><span class="n">c</span> <span class="o">=</span> <span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;h&#39;</span><span class="p">],</span><span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;k&#39;</span><span class="p">],</span><span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;c&#39;</span><span class="p">]</span>

    <span class="c"># convert nu to Hz</span>
    <span class="n">nu</span> <span class="o">=</span> <span class="n">nu</span> <span class="o">*</span> <span class="n">frequency_dict</span><span class="p">[</span><span class="n">frequency_units</span><span class="p">]</span>

    <span class="n">I</span> <span class="o">=</span> <span class="mi">2</span><span class="o">*</span><span class="n">h</span><span class="o">*</span><span class="n">nu</span><span class="o">**</span><span class="mi">3</span> <span class="o">/</span> <span class="n">c</span><span class="o">**</span><span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="n">h</span><span class="o">*</span><span class="n">nu</span><span class="o">/</span><span class="p">(</span><span class="n">k</span><span class="o">*</span><span class="n">temperature</span><span class="p">))</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span><span class="o">**-</span><span class="mi">1</span>

    <span class="k">if</span> <span class="n">normalize</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">I</span><span class="p">,</span><span class="s">&#39;__len__&#39;</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">I</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">I</span><span class="o">/</span><span class="n">normalize</span><span class="p">(</span><span class="n">I</span><span class="p">)</span> <span class="o">*</span> <span class="n">scale</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">I</span> <span class="o">*</span> <span class="n">scale</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">I</span> <span class="o">*</span> <span class="n">scale</span>
</div>
<span class="n">wavelength_dict</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;meters&#39;</span><span class="p">:</span><span class="mf">1.0</span><span class="p">,</span><span class="s">&#39;m&#39;</span><span class="p">:</span><span class="mf">1.0</span><span class="p">,</span>
        <span class="s">&#39;centimeters&#39;</span><span class="p">:</span><span class="mf">1e-2</span><span class="p">,</span><span class="s">&#39;cm&#39;</span><span class="p">:</span><span class="mf">1e-2</span><span class="p">,</span>
        <span class="s">&#39;millimeters&#39;</span><span class="p">:</span><span class="mf">1e-3</span><span class="p">,</span><span class="s">&#39;mm&#39;</span><span class="p">:</span><span class="mf">1e-3</span><span class="p">,</span>
        <span class="s">&#39;nanometers&#39;</span><span class="p">:</span><span class="mf">1e-9</span><span class="p">,</span><span class="s">&#39;nm&#39;</span><span class="p">:</span><span class="mf">1e-9</span><span class="p">,</span>
        <span class="s">&#39;micrometers&#39;</span><span class="p">:</span><span class="mf">1e-6</span><span class="p">,</span><span class="s">&#39;micron&#39;</span><span class="p">:</span><span class="mf">1e-6</span><span class="p">,</span><span class="s">&#39;microns&#39;</span><span class="p">:</span><span class="mf">1e-6</span><span class="p">,</span><span class="s">&#39;um&#39;</span><span class="p">:</span><span class="mf">1e-6</span><span class="p">,</span>
        <span class="s">&#39;kilometers&#39;</span><span class="p">:</span><span class="mf">1e3</span><span class="p">,</span><span class="s">&#39;km&#39;</span><span class="p">:</span><span class="mf">1e3</span><span class="p">,</span>
        <span class="s">&#39;angstroms&#39;</span><span class="p">:</span><span class="mf">1e-10</span><span class="p">,</span><span class="s">&#39;A&#39;</span><span class="p">:</span><span class="mf">1e-10</span><span class="p">,</span><span class="s">&#39;Angstroms&#39;</span><span class="p">:</span><span class="mf">1e-10</span><span class="p">,</span>
        <span class="p">}</span>

<div class="viewcode-block" id="blackbody_wavelength"><a class="viewcode-back" href="../../agpy.html#agpy.blackbody.blackbody_wavelength">[docs]</a><span class="k">def</span> <span class="nf">blackbody_wavelength</span><span class="p">(</span><span class="n">lam</span><span class="p">,</span><span class="n">temperature</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">units</span><span class="o">=</span><span class="s">&#39;cgs&#39;</span><span class="p">,</span><span class="n">wavelength_units</span><span class="o">=</span><span class="s">&#39;Angstroms&#39;</span><span class="p">,</span> <span class="n">normalize</span><span class="o">=</span><span class="nb">max</span><span class="p">):</span>
    <span class="c"># load constants in desired units</span>
    <span class="n">h</span><span class="p">,</span><span class="n">k</span><span class="p">,</span><span class="n">c</span> <span class="o">=</span> <span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;h&#39;</span><span class="p">],</span><span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;k&#39;</span><span class="p">],</span><span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;c&#39;</span><span class="p">]</span>

    <span class="c"># convert nu to Hz</span>
    <span class="n">lam</span> <span class="o">=</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">wavelength_dict</span><span class="p">[</span><span class="n">wavelength_units</span><span class="p">]</span> <span class="o">/</span> <span class="p">(</span><span class="mf">1e-2</span> <span class="k">if</span> <span class="n">units</span><span class="o">==</span><span class="s">&#39;cgs&#39;</span> <span class="k">else</span> <span class="mi">1</span><span class="p">)</span>

    <span class="n">I</span> <span class="o">=</span> <span class="mi">2</span><span class="o">*</span><span class="n">h</span><span class="o">*</span><span class="n">c</span><span class="o">**</span><span class="mi">2</span> <span class="o">/</span> <span class="n">lam</span><span class="o">**</span><span class="mi">5</span> <span class="o">*</span> <span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="n">h</span><span class="o">*</span><span class="n">c</span><span class="o">/</span><span class="p">(</span><span class="n">k</span><span class="o">*</span><span class="n">temperature</span><span class="o">*</span><span class="n">lam</span><span class="p">))</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span><span class="o">**-</span><span class="mi">1</span>

    <span class="k">if</span> <span class="n">normalize</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">I</span><span class="p">,</span><span class="s">&#39;__len__&#39;</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">I</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">I</span><span class="o">/</span><span class="n">normalize</span><span class="p">(</span><span class="n">I</span><span class="p">)</span> <span class="o">*</span> <span class="n">scale</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">I</span> <span class="o">*</span> <span class="n">scale</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">I</span> <span class="o">*</span> <span class="n">scale</span>
</div>
<div class="viewcode-block" id="modified_blackbody"><a class="viewcode-back" href="../../agpy.html#agpy.blackbody.modified_blackbody">[docs]</a><span class="k">def</span> <span class="nf">modified_blackbody</span><span class="p">(</span><span class="n">nu</span><span class="p">,</span><span class="n">temperature</span><span class="p">,</span><span class="n">beta</span><span class="o">=</span><span class="mf">1.75</span><span class="p">,</span> <span class="n">logscale</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">logN</span><span class="o">=</span><span class="mi">22</span><span class="p">,</span>
        <span class="n">muh2</span><span class="o">=</span><span class="mf">2.8</span><span class="p">,</span> <span class="n">units</span><span class="o">=</span><span class="s">&#39;cgs&#39;</span><span class="p">,</span><span class="n">frequency_units</span><span class="o">=</span><span class="s">&#39;Hz&#39;</span><span class="p">,</span> <span class="n">kappa0</span><span class="o">=</span><span class="mf">4.0</span><span class="p">,</span> <span class="n">nu0</span><span class="o">=</span><span class="mf">505e9</span><span class="p">,</span>
        <span class="n">normalize</span><span class="o">=</span><span class="nb">max</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Snu =  2hnu^3 c^-2  (e^(hnu/kT) - 1)^-1  (1 - e^(-tau_nu) )</span>
<span class="sd">    Kappa0 and Nu0 are set as per http://arxiv.org/abs/1101.4654 which uses OH94 values.</span>
<span class="sd">    beta = 1.75 is a reasonable default for Herschel data</span>
<span class="sd">    N = 1e22 is the column density in cm^-2</span>

<span class="sd">    nu0 and nu must have same units!</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">h</span><span class="p">,</span><span class="n">k</span><span class="p">,</span><span class="n">c</span> <span class="o">=</span> <span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;h&#39;</span><span class="p">],</span><span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;k&#39;</span><span class="p">],</span><span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;c&#39;</span><span class="p">]</span>
    <span class="n">mh</span> <span class="o">=</span> <span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;mh&#39;</span><span class="p">]</span>

    <span class="n">kappanu</span> <span class="o">=</span> <span class="n">kappa0</span> <span class="o">*</span> <span class="p">(</span><span class="n">nu</span><span class="o">/</span><span class="n">nu0</span><span class="p">)</span><span class="o">**</span><span class="n">beta</span>
    <span class="c"># numpy apparently can&#39;t multiply floats and longs</span>
    <span class="n">tau</span>  <span class="o">=</span> <span class="n">muh2</span> <span class="o">*</span> <span class="n">mh</span> <span class="o">*</span> <span class="n">kappanu</span> <span class="o">*</span> <span class="mf">10.0</span><span class="o">**</span><span class="n">logN</span>

    <span class="n">modification</span> <span class="o">=</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">-</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">1.0</span> <span class="o">*</span> <span class="n">tau</span><span class="p">))</span>

    <span class="n">I</span> <span class="o">=</span> <span class="n">blackbody</span><span class="p">(</span><span class="n">nu</span><span class="p">,</span><span class="n">temperature</span><span class="p">,</span><span class="n">units</span><span class="o">=</span><span class="n">units</span><span class="p">,</span><span class="n">frequency_units</span><span class="o">=</span><span class="n">frequency_units</span><span class="p">,</span><span class="n">normalize</span><span class="o">=</span><span class="n">normalize</span><span class="p">)</span><span class="o">*</span><span class="n">modification</span>

    <span class="k">if</span> <span class="n">normalize</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">I</span><span class="p">,</span><span class="s">&#39;__len__&#39;</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">I</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">I</span><span class="o">/</span><span class="n">normalize</span><span class="p">(</span><span class="n">I</span><span class="p">)</span> <span class="o">*</span> <span class="mf">10.</span><span class="o">**</span><span class="n">logscale</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">I</span> <span class="o">*</span> <span class="mf">10.</span><span class="o">**</span><span class="n">logscale</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">I</span> <span class="o">*</span> <span class="mf">10.</span><span class="o">**</span><span class="n">logscale</span>
</div>
<div class="viewcode-block" id="greybody"><a class="viewcode-back" href="../../agpy.html#agpy.blackbody.greybody">[docs]</a><span class="k">def</span> <span class="nf">greybody</span><span class="p">(</span><span class="n">nu</span><span class="p">,</span> <span class="n">temperature</span><span class="p">,</span> <span class="n">beta</span><span class="p">,</span> <span class="n">A</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">logscale</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span>
        <span class="n">units</span><span class="o">=</span><span class="s">&#39;cgs&#39;</span><span class="p">,</span> <span class="n">frequency_units</span><span class="o">=</span><span class="s">&#39;Hz&#39;</span><span class="p">,</span> 
        <span class="n">kappa0</span><span class="o">=</span><span class="mf">4.0</span><span class="p">,</span> <span class="n">nu0</span><span class="o">=</span><span class="mf">3000e9</span><span class="p">,</span> <span class="n">normalize</span><span class="o">=</span><span class="nb">max</span><span class="p">):</span>
    <span class="n">h</span><span class="p">,</span><span class="n">k</span><span class="p">,</span><span class="n">c</span> <span class="o">=</span> <span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;h&#39;</span><span class="p">],</span><span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;k&#39;</span><span class="p">],</span><span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;c&#39;</span><span class="p">]</span>
    <span class="n">mh</span> <span class="o">=</span> <span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;mh&#39;</span><span class="p">]</span>

    <span class="n">modification</span> <span class="o">=</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">-</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">nu</span><span class="o">/</span><span class="n">nu0</span><span class="p">)</span><span class="o">**</span><span class="n">beta</span><span class="p">))</span>
    <span class="n">I</span> <span class="o">=</span> <span class="n">blackbody</span><span class="p">(</span><span class="n">nu</span><span class="p">,</span><span class="n">temperature</span><span class="p">,</span><span class="n">units</span><span class="o">=</span><span class="n">units</span><span class="p">,</span><span class="n">frequency_units</span><span class="o">=</span><span class="n">frequency_units</span><span class="p">,</span><span class="n">normalize</span><span class="o">=</span><span class="n">normalize</span><span class="p">)</span><span class="o">*</span><span class="n">modification</span>

    <span class="k">if</span> <span class="n">normalize</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">I</span><span class="p">,</span><span class="s">&#39;__len__&#39;</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">I</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">I</span><span class="o">/</span><span class="n">normalize</span><span class="p">(</span><span class="n">I</span><span class="p">)</span> <span class="o">*</span> <span class="mf">10.</span><span class="o">**</span><span class="n">logscale</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">I</span> <span class="o">*</span> <span class="mf">10.</span><span class="o">**</span><span class="n">logscale</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">I</span> <span class="o">*</span> <span class="mf">10.</span><span class="o">**</span><span class="n">logscale</span>
</div>
<div class="viewcode-block" id="modified_blackbody_wavelength"><a class="viewcode-back" href="../../agpy.html#agpy.blackbody.modified_blackbody_wavelength">[docs]</a><span class="k">def</span> <span class="nf">modified_blackbody_wavelength</span><span class="p">(</span><span class="n">lam</span><span class="p">,</span> <span class="n">temperature</span><span class="p">,</span> <span class="n">beta</span><span class="o">=</span><span class="mf">1.75</span><span class="p">,</span> <span class="n">logscale</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span>
        <span class="n">logN</span><span class="o">=</span><span class="mi">22</span><span class="p">,</span> <span class="n">muh2</span><span class="o">=</span><span class="mf">2.8</span><span class="p">,</span> <span class="n">units</span><span class="o">=</span><span class="s">&#39;cgs&#39;</span><span class="p">,</span> <span class="n">wavelength_units</span><span class="o">=</span><span class="s">&#39;Angstroms&#39;</span><span class="p">,</span>
        <span class="n">kappa0</span><span class="o">=</span><span class="mf">4.0</span><span class="p">,</span> <span class="n">nu0</span><span class="o">=</span><span class="mf">3000e9</span><span class="p">,</span> <span class="n">normalize</span><span class="o">=</span><span class="nb">max</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Snu =  2hnu^3 c^-2  (e^(hnu/kT) - 1)^-1  (1 - e^(-tau_nu) )</span>
<span class="sd">    Kappa0 and Nu0 are set as per http://arxiv.org/abs/1101.4654 which uses OH94 values.</span>
<span class="sd">    beta = 1.75 is a reasonable default for Herschel data</span>
<span class="sd">    N = 1e22 is the column density in cm^-2</span>

<span class="sd">    nu0 and nu must have same units!</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">h</span><span class="p">,</span><span class="n">k</span><span class="p">,</span><span class="n">c</span> <span class="o">=</span> <span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;h&#39;</span><span class="p">],</span><span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;k&#39;</span><span class="p">],</span><span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;c&#39;</span><span class="p">]</span>
    <span class="n">mh</span> <span class="o">=</span> <span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;mh&#39;</span><span class="p">]</span>

    <span class="n">nu</span> <span class="o">=</span> <span class="n">c</span><span class="o">/</span><span class="p">(</span><span class="n">lam</span><span class="o">*</span><span class="n">wavelength_dict</span><span class="p">[</span><span class="n">wavelength_units</span><span class="p">]</span><span class="o">/</span><span class="n">wavelength_dict</span><span class="p">[</span><span class="n">unitdict</span><span class="p">[</span><span class="n">units</span><span class="p">][</span><span class="s">&#39;length&#39;</span><span class="p">]])</span>
    <span class="n">kappanu</span> <span class="o">=</span> <span class="n">kappa0</span> <span class="o">*</span> <span class="p">(</span><span class="n">nu</span><span class="o">/</span><span class="n">nu0</span><span class="p">)</span><span class="o">**</span><span class="n">beta</span>
    <span class="n">tau</span>  <span class="o">=</span> <span class="n">muh2</span> <span class="o">*</span> <span class="n">mh</span> <span class="o">*</span> <span class="n">kappanu</span> <span class="o">*</span> <span class="mf">10.</span><span class="o">**</span><span class="n">logN</span>

    <span class="n">modification</span> <span class="o">=</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">-</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">1.0</span> <span class="o">*</span> <span class="n">tau</span><span class="p">))</span>

    <span class="n">I</span> <span class="o">=</span> <span class="n">blackbody</span><span class="p">(</span><span class="n">nu</span><span class="p">,</span><span class="n">temperature</span><span class="p">,</span><span class="n">units</span><span class="o">=</span><span class="n">units</span><span class="p">,</span><span class="n">frequency_units</span><span class="o">=</span><span class="s">&#39;Hz&#39;</span><span class="p">,</span><span class="n">normalize</span><span class="o">=</span><span class="n">normalize</span><span class="p">)</span><span class="o">*</span><span class="n">modification</span>

    <span class="k">if</span> <span class="n">normalize</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">I</span><span class="p">,</span><span class="s">&#39;__len__&#39;</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">I</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">I</span><span class="o">/</span><span class="n">normalize</span><span class="p">(</span><span class="n">I</span><span class="p">)</span> <span class="o">*</span> <span class="mf">10.</span><span class="o">**</span><span class="n">logscale</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">I</span> <span class="o">*</span> <span class="mf">10.</span><span class="o">**</span><span class="n">logscale</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">I</span> <span class="o">*</span> <span class="mi">10</span><span class="o">**</span><span class="n">logscale</span>

</div>
<span class="k">try</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">agpy.mpfit</span> <span class="kn">as</span> <span class="nn">mpfit</span>

<div class="viewcode-block" id="fit_blackbody"><a class="viewcode-back" href="../../agpy.html#agpy.blackbody.fit_blackbody">[docs]</a>    <span class="k">def</span> <span class="nf">fit_blackbody</span><span class="p">(</span><span class="n">xdata</span><span class="p">,</span> <span class="n">flux</span><span class="p">,</span> <span class="n">guesses</span><span class="o">=</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">),</span> <span class="n">err</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">blackbody_function</span><span class="o">=</span><span class="n">blackbody</span><span class="p">,</span> <span class="n">quiet</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        guesses = Temperature, Arbitrary Scale</span>
<span class="sd">        OR Temperature, Beta, Arbitrary Scale</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">def</span> <span class="nf">mpfitfun</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">err</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">err</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="n">fjac</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> <span class="k">return</span> <span class="p">[</span><span class="mi">0</span><span class="p">,(</span><span class="n">y</span><span class="o">-</span><span class="n">blackbody_function</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="o">*</span><span class="n">p</span><span class="p">,</span> <span class="n">normalize</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">))]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="n">fjac</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> <span class="k">return</span> <span class="p">[</span><span class="mi">0</span><span class="p">,(</span><span class="n">y</span><span class="o">-</span><span class="n">blackbody_function</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="o">*</span><span class="n">p</span><span class="p">,</span> <span class="n">normalize</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">))</span><span class="o">/</span><span class="n">err</span><span class="p">]</span>
            <span class="k">return</span> <span class="n">f</span>

        <span class="n">err</span> <span class="o">=</span> <span class="n">err</span> <span class="k">if</span> <span class="n">err</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="k">else</span> <span class="n">flux</span><span class="o">*</span><span class="mf">0.0</span> <span class="o">+</span> <span class="mf">1.0</span>

        <span class="n">mp</span> <span class="o">=</span> <span class="n">mpfit</span><span class="o">.</span><span class="n">mpfit</span><span class="p">(</span><span class="n">mpfitfun</span><span class="p">(</span><span class="n">xdata</span><span class="p">,</span><span class="n">flux</span><span class="p">,</span><span class="n">err</span><span class="p">),</span> <span class="n">guesses</span><span class="p">,</span> <span class="n">quiet</span><span class="o">=</span><span class="n">quiet</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">mp</span></div>
<span class="k">except</span><span class="p">:</span>
    <span class="k">pass</span>
</pre></div>

          </div>
        </div>
      </div>
        </div>
        <div class="sidebar">
          <h3>Table Of Contents</h3>
          <ul>
<li class="toctree-l1"><a class="reference internal" href="../../agpy.html">Adam Ginsburg&#8217;s Python Code (agpy)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../image_tools.html">Image Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../fft_tools.html">AG_fft_tools Package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../plfit.html">plfit Package</a></li>
</ul>

          <h3 style="margin-top: 1.5em;">Search</h3>
          <form class="search" action="../../search.html" method="get">
            <input type="text" name="q" />
            <input type="submit" value="Go" />
            <input type="hidden" name="check_keywords" value="yes" />
            <input type="hidden" name="area" value="default" />
          </form>
          <p class="searchtip" style="font-size: 90%">
            Enter search terms or a module, class or function name.
          </p>
        </div>
        <div class="clearer"></div>
      </div>
    </div>

    <div class="footer">
      &copy; Copyright 2011, Adam Ginsburg.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2pre.
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-6253248-2']);
      _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();

    </script>
        
    </div>
  </body>
</html>